<?php

use console\db\Migration;

/**
 * 创建用户动态数据相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m180201_103000_t_user_trend extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 用户收藏内容表
         * @table `%user_favorite`.
         */
        $this->tableBuilder([
            'name' => 'user_favorite',
            'comment' => '用户收藏内容表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'model' => $this->char(20)->notNull()->comment('收藏模型ID'),
                'model_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('收藏内容ID'),
                'created_at' => $this->unixTimestamp()->comment('收藏时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ]
        ]);

        /**
         * 用户动态数据表
         * @table `user_trend`.
         */
        $this->tableBuilder([
            'name' => 'user_trend',
            'comment' => '用户动态数据表',
            'columns' => [
                'id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'level_id' => $this->integer(11)->notNull()->defaultValue(0)->comment('等级'),
                'experience' => $this->integer(11)->notNull()->defaultValue(0)->comment('经验值'),
                'credit' => $this->integer(11)->notNull()->defaultValue(0)->comment('积分'),
                'gold' => $this->integer(11)->notNull()->defaultValue(0)->comment('金币'),
                'cash' => $this->integer(11)->notNull()->defaultValue(0)->comment('可提现金币'),
                'liveness' => $this->integer(11)->notNull()->defaultValue(0)->comment('用户活力值'),
                'likes' => $this->integer(11)->notNull()->defaultValue(0)->comment('点赞数'),
                'gis' => $this->geometry()->comment('空间位置信息'),
                'geohash' => $this->geometryHash('gis'),
                'located_at' => $this->unixTimestamp()->comment('定位时间'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'PRIMARY KEY ([[id]])',
            ],
            'index' => [
                [
                    'columns' => 'id',
                    'unique' => true,
                ]
            ]
        ]);

        /**
         * 用户标签表
         * @table `user_tag`.
         */
        $this->tableBuilder([
            'name' => 'user_tag',
            'comment' => '用户标签表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'tag_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('标签ID'),
                'created_at' => $this->unixTimestamp()->comment('获得时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);

        /**
         * 用户发布内容关系表
         * @table `user_publish`.
         */
        $this->tableBuilder([
            'name' => 'user_publish',
            'comment' => '用户发布内容关系表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'dynamic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('主题ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'reply_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('回复ID'),
                'publish_at' => $this->unixTimestamp()->comment('发布时间'),
                'created_at' => $this->unixTimestamp()->comment('获得时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);

        /**
         * 用户任务完成表
         * @table `user_task`.
         */
        $this->tableBuilder([
            'name' => 'user_task',
            'comment' => '用户任务完成表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'task_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('任务ID'),
                'date' => $this->date()->comment('日期'),
                'times' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('任务重复次数'),
                'status' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(0)->comment('完成状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);

        /**
         * 用户获得奖励表
         * @table `user_award`.
         */
        $this->tableBuilder([
            'name' => 'user_award',
            'comment' => '用户获得奖励表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'from' => $this->char(20)->notNull()->comment('奖励来源'),
                'from_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('来源ID'),
                'prize_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('奖励内容'),
                'prize_value' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('奖励数值'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('领取状态'),
                'expired_at' => $this->unixTimestamp()->comment('过期时间'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%user_award}}');
        $this->dropTable('{{%user_task}}');
        $this->dropTable('{{%user_publish}}');
        $this->dropTable('{{%user_tag}}');
        $this->dropTable('{{%user_trend}}');
        $this->dropTable('{{%user_favorite}}');
    }

}
